package cn.chinasyq.photoquan.util;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.view.SweetAlertDialog.SweetAlertDialog;


public class DialogUtil {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static Dialog createProgressDialog(Context c) {
        Dialog builder = new Dialog(c, R.style.add_dialog);
        View v = LayoutInflater.from(c).inflate(R.layout.layout_progress_dialog, null);

        final View icon = v.findViewById(R.id.icon_dialog);
        final RotateAnimation rotateAni = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAni.setDuration(1000);
        rotateAni.setRepeatCount(-1);
        LinearInterpolator lirInterpolator = new LinearInterpolator();
        rotateAni.setInterpolator(lirInterpolator);
        rotateAni.setFillAfter(true);
        rotateAni.setRepeatMode(Animation.RESTART);
        icon.post(new Runnable() {
            @Override
            public void run() {
                rotateAni.start();
                icon.setAnimation(rotateAni);
            }
        });
        builder.setContentView(v);

        return builder;
    }

    public static SweetAlertDialog showAskDialog(Context context, String title, String msg, final DialogInterface.OnClickListener oklistener,
                                                 boolean isnoactivity) {

        SweetAlertDialog dialog = new SweetAlertDialog(context);
        dialog.setTitleText(title);
        dialog.setContentText(msg);
        dialog.setCancelText(context.getString(R.string.cancel));
        dialog.setConfirmText(context.getString(android.R.string.ok));
        dialog.setCancelable(false);
        if (isnoactivity) {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        dialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {

            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        });
        dialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sDialog) {
                sDialog.dismiss();
                oklistener.onClick(sDialog, 0);
            }
        });
        dialog.show();
        return dialog;
    }
}
